package selenium;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

import org.openqa.selenium.By;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

public class qiyezhanghao {
//帐号自动记录
	public static void main(String[] args) {
		final WebDriver driver = new ChromeDriver();
		try {
			driver.manage().window().maximize();
			Date now = new Date();// 获取系统当前时间
			SimpleDateFormat sDate = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");// 格式化日期，和数据库中日期格式相同，这里是datetime
			String strDate = sDate.format(now);// 格式化日期转换为字符串
			String filepath = "D:\\ZhangHao";// 文件夹路径
			File files = new File(filepath);
			if (!files.exists()) {
				files.mkdirs();
			};
			for (int i = 0; i < 300; i++) {
				driver.manage().deleteAllCookies();
				driver.get("http://mp.163.com/login.html");
				System.out.println("请求发送成功");
				final StringBuffer s = new StringBuffer();
				(new WebDriverWait(driver, 500)).until(new ExpectedCondition<WebElement>() {
					@Override
					public WebElement apply(WebDriver arg0) {
						WebElement suuDriver = driver
								.findElement(By.xpath("//*[@id='root']/div"));
						if (suuDriver != null) {
							return suuDriver;
						} else {
							return null;
						}
					}
				});

				// ------start--帐号分类--邮箱帐号--
				driver.get("http://mp.163.com/info.html");
				(new WebDriverWait(driver, 500)).until(new ExpectedCondition<WebElement>() {
					@Override
					public WebElement apply(WebDriver arg0) {
						
						WebElement mingChengDriver = driver.findElement(By.xpath("//*[@id='accountForm']/div[1]/div[1]/p"));
						String mingCheng = mingChengDriver.getText().toString().trim();
						s.append(mingCheng + "/");
						WebElement leiDriver = driver.findElement(By.xpath("//*[@id='accountForm']/div[3]/div/p"));
						if (leiDriver != null) {
							// ------start--帐号分类--邮箱帐号--
							String fenLei = leiDriver.getText().toString().trim();
							System.out.println("分类：" + fenLei);
							s.append(fenLei + "/");
							//*[@id="accountForm"]/div[13]/div[1]/p
							System.out.println("邮箱监测点1");
							WebElement zhangHaoDriver = driver
									.findElement(By.xpath("//*[@id='accountForm']/div[18]/div[1]/p"));
							System.out.println("邮箱监测点2");
							String zhanghao = zhangHaoDriver.getText().toString().replaceAll("\"", "").trim();
							System.out.println("邮箱帐号：" + zhanghao);
							s.insert(0, zhanghao + "/");
							// ------end--帐号分类--邮箱帐号--
							return leiDriver;
						} else {
							return null;
						}
					}
				});
				// ------end--帐号分类--邮箱帐号--

				Set<Cookie> cookies = driver.manage().getCookies();
				for (Cookie cookie : cookies) {
					String cookieName = cookie.getName();
					String cookieValue = cookie.getValue();
					if ("NTES_SESS".equals(cookieName)) {
						System.out.println(cookieValue);
						s.append(cookieValue);
					}
				}
				String zhangHaoString = s.toString();
				System.out.println(zhangHaoString);
				File fileExist = new File(filepath + "/" + strDate + ".txt");
				if (!fileExist.exists()) {
					fileExist.createNewFile();
				}
				method2(filepath + "/" + strDate + ".txt", zhangHaoString);
				driver.manage().deleteAllCookies();

			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		driver.quit();
	}

	// 方法2：

	public static void method2(String file, String conent) {
		BufferedWriter out = null;
		try {
			out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true)));
			out.write(conent + "\r\n");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public static List<String> readFile(String filePath, String str) throws IOException {
		File file = new File(filePath);
		BufferedReader reader = null;
		List<String> list = new ArrayList<String>();
		reader = new BufferedReader(new FileReader(file));
		String text = reader.readLine();
		while (text != null) {
			if (!text.trim().equals(str)) {
				list.add(text + "\r\n");
			}
			text = reader.readLine();
		}
		reader.close();
		return list;
	}

	public static void writeFile(String filePath, List<String> list) throws IOException {
		File file = new File(filePath);
		if (!file.exists()) {
			file.createNewFile();
		}
		FileOutputStream outputStream = new FileOutputStream(file);
		for (String s : list) {
			outputStream.write(s.getBytes());
		}
		outputStream.close();
	}

}
